package com.bkgy.controller.technology.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bkgy.commom.model.QueryDTO;
import com.bkgy.controller.repTechFit.service.IRepTechFitService;
import com.bkgy.controller.technology.entity.TTech;
import com.bkgy.controller.technology.entity.dto.*;
import com.bkgy.controller.technology.service.*;
import com.bkgy.sys.common.entity.ResultData;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * 工艺参数
 */
@Tag(name = "工艺参数", description = "工艺参数")
@RestController
@RequiredArgsConstructor
@RequestMapping("/technology")
public class TechController {
    private final ITechService techService;
    private final ICalProcessService calService;
    private final ICgl1870ProcessService cglService;
    private final ITcm2280ProcessService tcmService;
    private final IRepTechFitService techFitService;

    @Operation(summary = "工艺参数总览列表", description = "工艺参数》总览列表")
    @PostMapping("/techOverview")
    @ResponseBody
    public ResultData techOverview(@RequestBody TechQueryDto tc) {
        String startDate = tc.getStartDate().replaceAll("-", "");
        String endDate = tc.getEndDate().replaceAll("-", "");
        if ("cgl1870".equals(tc.getLineCode())) {
            return null;
        } else if ("cal1630".equals(tc.getLineCode()) || "cal2150".equals(tc.getLineCode())) {
            return null;
        } else {
            return tcmService.techOverview(startDate, endDate);
        }
    }

    @Operation(summary = "工艺参数不合格数量柱形图", description = "工艺参数》总览》工艺不合格量柱形图")
    @PostMapping("/techFailBar")
    @ResponseBody
    public ResultData<TechFailBarRes> techFailBar(@RequestBody TechQueryDto tc) {
        String startDate = tc.getStartDate().replaceAll("-", "");
        String endDate = tc.getEndDate().replaceAll("-", "");
        if ("cgl1870".equals(tc.getLineCode())) {
            return null;
        } else if ("cal1630".equals(tc.getLineCode()) || "cal2150".equals(tc.getLineCode())) {
            return null;
        } else {
            return techFitService.techFailBar(startDate, endDate);
        }
    }

    @Operation(summary = "工艺参数不合格数量周、月统计折线图", description = "工艺参数》总览》工艺不合格量周、月统计")
    @PostMapping("/techFailLine")
    @ResponseBody
    public ResultData techFailLine(@RequestBody @Valid TechLineDate tc) {
        if ("cgl1870".equals(tc.getLineCode())) {
            return null;
        } else if ("cal1630".equals(tc.getLineCode()) || "cal2150".equals(tc.getLineCode())) {
            return null;
        } else {
            return techFitService.techFailLine(tc.getDateType());
        }
    }

    @Operation(summary = "工艺参数符合率列表", description = "工艺参数》符合率列表")
    @PostMapping("/techFitRate")
    @ResponseBody
    public ResultData techFitRate(@RequestBody TechQueryDto tc) {
        String startDate = tc.getStartDate().substring(0, 10).replaceAll("-", "");
        String endDate = tc.getEndDate().substring(0, 10).replaceAll("-", "");
        if ("cgl1870".equals(tc.getLineCode())) {
            return null;
        } else if ("cal1630".equals(tc.getLineCode()) || "cal2150".equals(tc.getLineCode())) {
            return null;
        } else {
            return tcmService.techFitRate(startDate, endDate);
        }
    }

    @Operation(summary = "工艺参数符合率已生产卷合格率", description = "工艺参数》符合率》已生产卷合格率")
    @PostMapping("/techSucRate")
    @ResponseBody
    public ResultData techSucRate(@RequestBody TechQueryDto tc) {
        String startDate = tc.getStartDate().substring(0, 10).replaceAll("-", "");
        String endDate = tc.getEndDate().substring(0, 10).replaceAll("-", "");
        if ("cgl1870".equals(tc.getLineCode())) {
            return null;
        } else if ("cal1630".equals(tc.getLineCode()) || "cal2150".equals(tc.getLineCode())) {
            return null;
        } else {
            return techFitService.techSucRate(startDate, endDate);
        }
    }

    @Operation(summary = "加载产线最新的20条卷号", description = "加载产线最新的20条卷号")
    @PostMapping("/loadRoll")
    public ResultData<String> loadRoll(@RequestBody TechQueryDto queryDTO) {
        if (StringUtils.isNotEmpty(queryDTO.getLineCode())) {
            return techService.loadRoll(queryDTO.getLineCode());
        } else {
            return ResultData.ResultDataFaultGd("产线参数不能为空", "");
        }
    }

    @Operation(summary = "工艺参数导出", description = "工艺参数导出")
    @PostMapping("/exportXlsx")
    public void exportXlsx(@RequestBody TechQueryDto techReq, HttpServletResponse res) {
        techService.exportXlsx(techReq, res);
    }

    @Operation(summary = "生产效益分析/本班效益预估", description = "本班效益预估")
    @PostMapping("/techCrewProfit")
    @ResponseBody
    public ResultData<JSONObject> techCrewProfit() {
        return techService.techCrewProfit();
    }

    @Operation(summary = "生产效益分析/成本异常分析", description = "成本异常分析")
    @PostMapping("/techExecList")
    @ResponseBody
    public ResultData<TechExecDtoRes> techExecList() {
        return techService.techExecList();
    }

    @Operation(summary = "生产效益分析/班组效益对比", description = "班组效益对比")
    @PostMapping("/techCrewProfitList")
    @ResponseBody
    public ResultData<TechCrewProfitDtoRes> techCrewProfitList() {
        return techService.techCrewProfitList();
    }

    @Operation(summary = "生产效益分析/牌号生产效益", description = "牌号生产效益")
    @PostMapping("/techBrandProfitList")
    @ResponseBody
    public ResultData<TechBrandProfitDtoRes> techBrandProfitList() {
        return techService.techBrandProfitList();
    }

    @Operation(summary = "工艺参数符合率卷号列表", description = "工艺参数符合率列表")
    @PostMapping("/techList")
    @ResponseBody
    public ResultData<TechListDtoRes> techList(@RequestBody @Valid TechChartDtoReq tc) {
        return techService.selectTechList(tc);
    }

    @Operation(summary = "镀锌、连退、酸轧工艺参数符合率图表", description = "6个方块图表")
    @PostMapping("/techFit")
    @ResponseBody
    public ResultData techFit(@RequestBody @Valid TechChartDtoReq tc) {
        if ("cgl1870".equals(tc.getProdLine())) {
            return techService.selectTechFit(tc);
        } else if ("cal1630".equals(tc.getProdLine()) || "cal2150".equals(tc.getProdLine())) {
            return calService.selectTechFit(tc);
        } else {
            return tcmService.selectTechFit(tc);
        }
    }

    @Operation(summary = "镀锌、连退、酸轧生产工艺滚动信息统计", description = "生产工艺下滚动统计信息")
    @PostMapping("/rollInfo")
    @ResponseBody
    public ResultData<String> rollInfo(@RequestBody @Valid TechChartDtoReq tc) {
        if ("cgl1870".equals(tc.getProdLine())) {
            return cglService.rollInfo(tc);
        } else if ("cal1630".equals(tc.getProdLine()) || "cal2150".equals(tc.getProdLine())) {
            return calService.rollInfo(tc);
        } else {
            return tcmService.rollInfo(tc);
        }
    }

    @Operation(summary = "工艺参数列表", description = "工艺参数列表")
    @PostMapping("/page")
    public ResultData<Page<TTech>> page(@RequestBody @Valid QueryDTO<TechQueryDto> queryDTO) {
        return techService.pageTech(queryDTO);
    }

    @Operation(summary = "新增", description = "工艺参数添加")
    @PostMapping("/insert")
    @ResponseBody
    public ResultData<String> insert(@Parameter(description = "新增工艺参数") @RequestBody @Valid TTech tech) {
        tech.setIndocno(null);
        return techService.saveTech(tech);
    }

    @Operation(summary = "修改", description = "工艺参数修改")
    @PostMapping("/update")
    @ResponseBody
    public ResultData<String> update(@RequestBody @Valid TTech tech) {
        return techService.updateTech(tech);
    }

    @Operation(summary = "工艺参数删除", description = "工艺参数删除")
    @PostMapping("/delete")
    @ResponseBody
    public ResultData<String> delete(@NotNull Integer indocno) {
        return techService.deleteTech(indocno);
    }
}
